home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS15.ADF / PopCLI2 / WBC.a < prev    next >
Text File  |  1988-04-20  |  7KB  |  297 lines

  1.     section    text,CODE
  2. * C initial startup procedure under AmigaDOS
  3. * Requirements:
  4.  
  5.     INCLUDE    "exec/types.i"
  6.     INCLUDE "exec/alerts.i"
  7.     INCLUDE "exec/nodes.i"
  8.     INCLUDE "exec/lists.i"
  9.     INCLUDE "exec/ports.i"
  10.     INCLUDE "exec/libraries.i"
  11.     INCLUDE "exec/tasks.i"
  12.     INCLUDE "libraries/dos.i"
  13.     INCLUDE "libraries/dosextens.i"
  14.     INCLUDE "workbench/startup.i"
  15.  
  16. ; some usefull macros:
  17. xlib    macro
  18.     xref    _LVO\1
  19.     endm
  20.  
  21. callsys    macro
  22.     CALLLIB    _LVO\1
  23.     endm
  24.     
  25.  
  26.     xdef    _XCEXIT            * exit(code) is standard way to leave C.
  27.     xdef    _HandlerInterface
  28.  
  29.     xref    _LinkerDB        * linker defined base value
  30.  
  31.     xref    _Debug
  32.     xref    __main            * Name of C program to start with.
  33.     xref    _MemCleanup
  34.     xref    _AbsExecBase
  35.     xref    _FindTask
  36.     xref    _DOSBase
  37.     xref    _MathBase
  38.     xref    _MathTransBase
  39.         xref    _myhandler
  40.  
  41.     xlib    Alert
  42.     xlib    FindTask
  43.     xlib    Forbid
  44.     xlib    GetMsg
  45.     xlib    OpenLibrary
  46.     xlib    CloseLibrary
  47.     xlib    ReplyMsg
  48.     xlib    Wait
  49.     xlib    WaitPort
  50.     xlib    Open
  51.     xlib    Close
  52.     xlib    CurrentDir
  53.  
  54. start:
  55.     move.l    d0,dosCmdLen
  56.     move.l    a0,dosCmdBuf
  57.     move.l    a7,d0            ; save old stack ptr
  58.     movem.l    d1-d6/a0-a6,-(a7)
  59.     move.l    d0,a5
  60.     move.l    _AbsExecBase,a6
  61.     move.l    a6,_SysBase
  62.     move.l    a7,__StackPtr        * Save stack ptr
  63.     clr.l    _WBenchMsg
  64.  
  65. ;------ get the address of our task
  66.     suba.l    a1,a1
  67.     callsys    FindTask
  68.     move.l    d0,a4
  69.  
  70. ;------ are we running as a son of Workbench?
  71.     move.l    pr_CurrentDir(A4),_curdir
  72.     tst.l    pr_CLI(A4)
  73.     beq    fromWorkbench
  74.  
  75. ;=======================================================================
  76. ;====== CLI Startup Code ===============================================
  77. ;=======================================================================
  78.  
  79. fromCLI:
  80.     move.l    a5,D0        ; get top of stack
  81.     sub.l    4(a5),D0    ; compute bottom 
  82.     move.l    D0,__base    ; save for stack checking
  83. ;------    attempt to open DOS library:
  84.     bsr    openDOS
  85.  
  86. ;------ find command name:
  87.     move.l    #start,a0
  88.     clr.l    -(sp)
  89.     jsr    _FindTask
  90.     addq.l    #4,sp
  91.     move.l    d0,a0
  92.     move.l    pr_CLI(a0),a0
  93.     add.l   a0,a0        ; bcpl pointer conversion
  94.     add.l   a0,a0
  95.     move.l    cli_CommandName(a0),a1
  96.     add.l   a1,a1        ; bcpl pointer conversion
  97.     add.l   a1,a1
  98.  
  99. ;------    collect parameters:
  100.     move.l    dosCmdLen,d0        ; get command line length
  101.     move.l    a1,__ProgramName
  102.     addq.l    #1,d0            ; allow for space after command    
  103.  
  104.     clr.w    -(A7)            ; set null terminator for command line
  105.     addq.l    #1,D0            ; force to even number of bytes
  106.     andi.w    #$fffe,D0        ;(round up)
  107.     sub.l    D0,A7            ; make room on stack for command line
  108.     subq.l    #2,D0
  109.     clr.w    0(A7,D0)
  110.  
  111. ;------ copy command line onto stack
  112.     move.l    dosCmdLen,d0        ; get command line length
  113.     move.l    dosCmdBuf,a0
  114.     move.l    d0,d2
  115.     subq.l    #1,d0
  116.  
  117. copy_line:
  118.     move.b    0(A0,D0.W),0(A7,D2.W)    ; copy command line to stack
  119.     subq.l    #1,d2
  120.     dbf    d0,copy_line
  121.     move.b    #' ',0(a7,d2.w)        ; add space between command and parms
  122.     subq.l    #1,d2
  123.  
  124. copy_cmd:
  125.     move.l    A7,A1
  126.     move.l    A1,-(A7)        ; push command line address
  127.     bra    goto_main                * call C entrypoint
  128.  
  129. ;=======================================================================
  130. ;====== Workbench Startup Code =========================================
  131. ;=======================================================================
  132.  
  133. fromWorkbench:
  134.  
  135.     move.l    TC_SPLOWER(a4),__base    ; set base of stack
  136. ;------ open the DOS library:
  137.     bsr    openDOS
  138.  
  139. ;------ we are now set up.  wait for a message from our starter
  140.     bsr    waitmsg
  141.     move.l    d0,_WBenchMsg
  142.     move.l    d0,-(SP)
  143. ;
  144.     move.l    d0,a2            ; get first argument
  145.     move.l    sm_ArgList(a2),d0
  146.     beq.s    do_cons
  147.     move.l    _DOSBase,a6
  148.     move.l    d0,a0
  149.     move.l    wa_Lock(a0),d1
  150.     move.l    d1,_curdir
  151.     callsys    CurrentDir
  152. do_cons:
  153.     move.l    sm_ToolWindow(a2),d1    ; get the window argument
  154.     beq.s    do_main
  155.     move.l    #MODE_OLDFILE,d2
  156.     callsys    Open
  157.     move.l    d0,stdin
  158.     beq.s    do_main
  159.     lsl.l    #2,d0
  160.     move.l    d0,a0
  161.     move.l    fh_Type(a0),pr_ConsoleTask(A4)
  162. do_main:
  163.     move.l    _WBenchMsg,a0        ; get address of workbench message
  164.     move.l    sm_ArgList(a0),a0    ; get address of arguments
  165.     move.l    wa_Name(a0),__ProgramName    ; get name of program
  166.     moveq    #0,d0
  167.     move.l    d0,-(A7)
  168.  
  169. goto_main:
  170.     lea    _LinkerDB,a6        ; load base register
  171.     jsr    __main                * call C entrypoint
  172.     moveq.l    #0,d0            ; set successful status
  173.     bra.s    exit2
  174. ;
  175.  
  176. _XCEXIT:
  177.     move.l    4(SP),d0    ; extract return code
  178. exit2:
  179.     move.l    d0,-(a7)
  180.     move.l    _SysBase,a6
  181.     move.l    _DOSBase,a1
  182.     callsys    CloseLibrary        ; close Dos library
  183. done_1b:
  184. ;------ if we ran from CLI, skip workbench cleanup:
  185.     tst.l    _WBenchMsg
  186.     beq.s    exitToDOS
  187.     move.l    _console_dev,d1
  188.     beq.s    done_2
  189.     callsys    Close
  190. done_2:
  191.     move.l    stdin,d1
  192.     beq.s    done_4
  193.     callsys    Close
  194. done_4:
  195.  
  196. ;------ return the startup message to our parent
  197. ;    we forbid so workbench can't UnLoadSeg() us
  198. ;    before we are done:
  199.     move.l    _AbsExecBase,A6
  200.     callsys Forbid
  201.     move.l    _WBenchMsg,a1
  202.     callsys    ReplyMsg
  203.  
  204. ;------ this rts sends us back to DOS:
  205. exitToDOS:
  206.     MOVE.L    (A7)+,D0
  207.     movea.l  __StackPtr,SP        * restore stack ptr
  208.     movem.l    (a7)+,d1-d6/a0-a6
  209.     rts                * and exit
  210.  
  211. ;-----------------------------------------------------------------------
  212. noDOS:
  213.         ALERT    (AG_OpenLib!AO_DOSLib)
  214.         moveq.l    #100,d0
  215.         bra    exit2
  216.  
  217.  
  218. ;-----------------------------------------------------------------------
  219. ; This routine gets the message that workbench will send to us
  220. ; called with task id in A4
  221.  
  222. waitmsg:
  223.         lea    pr_MsgPort(A4),a0      * our process base
  224.         callsys    WaitPort
  225.         lea    pr_MsgPort(A4),a0      * our process base
  226.         callsys GetMsg
  227.         rts
  228.  
  229. ;-----------------------------------------------------------------------
  230. ;  Open the DOS library:
  231.  
  232. openDOS
  233.         lea    DOSName,A1
  234.         moveq.l    #0,D0
  235.         callsys OpenLibrary
  236.         move.l    D0,_DOSBase
  237.         beq    noDOS
  238.         rts
  239.  
  240. *************************************************************************
  241. *   HandlerInterface()
  242. *
  243. *   This code is needed to convert the calling sequence performed by
  244. *   the input.task for the input stream management into something
  245. *   that a C program can understand.
  246. *
  247. *   This routine expects a pointer to an InputEvent in A0, a pointer
  248. *   to a data area in A1.  These values are transferred to the stack
  249. *   in the order that a C program would need to find them.  Since the
  250. *   actual handler is written in C, this works out fine. 
  251. *
  252. *   Author: Rob Peck, 12/1/85
  253. *
  254.  
  255. _HandlerInterface:
  256.     movem.L    A0/A1,-(A7)
  257.     jsr    _myhandler
  258.     addq.L    #8,A7
  259.     rts
  260.  
  261.     section    "__MERGED",DATA
  262. ;
  263.     XDEF    _NULL,_SysBase,_LoadAddress,_console_dev,_WBenchMsg
  264.     XDEF    _curdir,__mbase,__mnext,__msize,__tsize
  265.     XDEF    __oserr,__OSERR,__FPERR,__SIGFPE,__ONERR,__ONEXIT,__ONBREAK
  266.     XDEF    __SIGINT,__ECS
  267.     XDEF    __ProgramName,__StackPtr,__base
  268. ;
  269. _NULL        dc.l    0        ;
  270. __base        dc.l    0        ; base of stack
  271. __mbase        dc.l    0        ; base of memory pool
  272. __mnext        dc.l    0        ; next available memory location
  273. __msize        dc.l    0        ; size of memory pool
  274. __tsize        dc.l    0        ; total size?
  275. __oserr        equ    *
  276. __OSERR        dc.l    0
  277. __FPERR        dc.l    0
  278. __SIGFPE    dc.l    0
  279. __SIGINT    dc.l    0
  280. __ONERR        dc.l    0
  281. __ONEXIT    dc.l    0
  282. __ONBREAK    dc.l    0
  283. __ECS        dc.l    0        ; extended character set flag
  284. _curdir        dc.l    0
  285. _console_dev    dc.l    0
  286. _SysBase    dc.l    0
  287. _LoadAddress    dc.l    0            ; program load address
  288. _WBenchMsg    dc.l    0
  289. __StackPtr    dc.l    0
  290. dosCmdLen    dc.l    0
  291. dosCmdBuf    dc.l    0
  292. stdin        dc.l    0
  293. __ProgramName    dc.l    0
  294. DOSName     DOSNAME
  295.  
  296.     END